package com.hong.taotaomall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hong.taotaomall.common.BaseContext;
import com.hong.taotaomall.entity.AddressBook;
import com.hong.taotaomall.entity.District;
import com.hong.taotaomall.entity.Result;
import com.hong.taotaomall.entity.ShoppingCart;
import com.hong.taotaomall.service.IAddressBookService;
import com.hong.taotaomall.service.IDistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author hong
 * @since 2023-03-25
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    @Autowired
    private IDistrictService districtService;

        @PostMapping
        public Result<String> save(@RequestBody AddressBook addressBook) {
            Long id = BaseContext.getCurrentId();
            addressBook.setUserId(id);
            int size = addressBookService.list(new LambdaQueryWrapper<AddressBook>()
                    .eq(AddressBook::getUserId, id)).size();
            if ( size > 6 ) {
                return Result.error("地址最多保存6条");
            }

                LambdaQueryWrapper<District> provinceLqw = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<District> cityLqw = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<District> districtLqw = new LambdaQueryWrapper<>();
            District province = districtService.getOne(
                    provinceLqw.eq(
                            District::getCode, addressBook.getProvinceCode()));
            String provinceName = province.getName();

            District city = districtService.getOne(
                    cityLqw.eq(
                            District::getCode, addressBook.getCityCode()));
            String cityName = city.getName();
            District district = districtService.getOne(
                    districtLqw.eq(
                            District::getCode, addressBook.getDistrictCode()));
            String districtName = district.getName();
            addressBook.setProvinceName(provinceName);
            addressBook.setCityName(cityName);
            addressBook.setDistrictName(districtName);
            if(addressBook.getIsDefault() == 1){
                LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
                wrapper.set(AddressBook::getIsDefault, 0);
                addressBookService.update(wrapper);
            }
                    addressBookService.save(addressBook);
            return Result.success("地址保存成功");
        }

        /**
         * 设置默认地址
         */
        @PutMapping("default")
        public Result<String> setDefault(@RequestParam Long id) {
            LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
            wrapper.set(AddressBook::getIsDefault, 0);
            addressBookService.update(wrapper);

            AddressBook addressBook = addressBookService.getOne(
                    new LambdaQueryWrapper<AddressBook>()
                            .eq(AddressBook::getId, id));
            addressBook.setIsDefault(1);
            addressBookService.updateById(addressBook);
            return Result.success("地址修改成功");
        }


        /**
         * 根据id查询地址
         */
        @GetMapping("/{id}")
        public Result get(@PathVariable Long id) {
            AddressBook addressBook = addressBookService.getById(id);
            if (addressBook != null) {
                return Result.success(addressBook);
            } else {
                return Result.error("没有找到该对象");
            }
        }

        /**
         * 查询默认地址
         */


        @PostMapping("/update")
        public Result<String> update(@RequestBody AddressBook addressBook) {
            Long id = BaseContext.getCurrentId();
            addressBook.setUserId(id);
            int size = addressBookService.list(new LambdaQueryWrapper<AddressBook>()
                    .eq(AddressBook::getUserId, id)).size();
            if ( size > 6 ) {
                return Result.error("地址最多保存6条");
            }
            if(addressBook.getIsDefault() == 1){
                LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
                wrapper.set(AddressBook::getIsDefault, 0);
                addressBookService.update(wrapper);
            }
            addressBookService.updateById(addressBook);
            return Result.success("购物车修改成功");
        }

        @GetMapping("default")
        public Result<AddressBook> getDefault() {
            LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
            queryWrapper.eq(AddressBook::getIsDefault, 1);
            AddressBook addressBook = addressBookService.getOne(queryWrapper);

            if (null == addressBook) {
                return Result.error("没有找到该对象");
            } else {
                return Result.success(addressBook);
            }
        }

        /**
         * 查询指定用户的全部地址
         */
        @GetMapping("/list")
        public Result<List<AddressBook>> list(AddressBook addressBook) {
            addressBook.setUserId(BaseContext.getCurrentId());
            LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
            queryWrapper.orderByDesc(AddressBook::getIsDefault);
            queryWrapper.orderByDesc(AddressBook::getUpdateTime);

            //SQL:select * from address_book where user_id = ? order by update_time desc
            return Result.success(addressBookService.list(queryWrapper));
        }

        @DeleteMapping
        public Result<String> delete(Long ids) {
            addressBookService.removeById(ids);
            return Result.success("地址删除成功");
        }
}
